package com.mzkj.wanjulaile.mobile.api.modules.address.controller;

import com.mzkj.common.data.StatusCode;
import com.mzkj.common.utils.StringUtils;
import com.mzkj.wanjulaile.mobile.api.common.controller.BaseController;
import com.mzkj.wanjulaile.mobile.api.common.entity.ResultEntity;
import com.mzkj.wanjulaile.mobile.api.modules.account.entity.Account;
import com.mzkj.wanjulaile.mobile.api.modules.address.entity.Address;
import com.mzkj.wanjulaile.mobile.api.modules.address.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "/auth/address")
public class AddressAuthController extends BaseController {
    @Autowired
    private AddressService addressService;

    /**
     * 获取地址列表
     * @param request  accessToken
     * @return
     */
    @RequestMapping("/addressInfo")
    @ResponseBody
    public ResultEntity<List<Address>> addressInfo(HttpServletRequest request){
        ResultEntity<List<Address>> result = new ResultEntity<List<Address>>();
        String accountId=(String)request.getAttribute("accountId");
        try {
            List<Address> addressList = addressService.getListByAccountId(accountId);
            result.setStatusCode(StatusCode.SC0.getValue());
            result.setMsg(StatusCode.SC0.getDesc());
            result.setData(addressList);
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatusCode(StatusCode.SC4002.getValue());
            result.setMsg(StatusCode.SC4002.getDesc());
        }
        return result;
    }

    /**
     *  设置默认地址
     * @param id  地址id
     * @param request  accessToken
     * @return
     */
    @ResponseBody
    @RequestMapping("/setDefault")
    public ResultEntity<String> setDefault(String id,HttpServletRequest request){
        ResultEntity<String> result = new ResultEntity<>();
        Map<String, Object> map = new HashMap<String, Object>();
        String accountId=(String)request.getAttribute("accountId");
        try {
            map.put("accountId", accountId);
            map.put("asDefault", 0);
            addressService.updateDefault(map);
            map.clear();
            map.put("id", id);
            map.put("asDefault", 1);
            addressService.updateDefault(map);
            //设置成功
            result.setStatusCode(StatusCode.SC0.getValue());
            result.setMsg(StatusCode.SC0.getDesc());
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatusCode(StatusCode.SC5000.getValue());
            result.setMsg(StatusCode.SC5000.getDesc());
        }
        return result;
    }

    /**
     * 删除地址
     * @param id  地址id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResultEntity<String> delete(String id){
        ResultEntity<String> result = new ResultEntity<>();
        try {
            addressService.delete(new Address(id));
            result.setStatusCode(StatusCode.SC0.getValue());
            result.setMsg(StatusCode.SC0.getDesc());
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatusCode(StatusCode.SC5000.getValue());
            result.setMsg(StatusCode.SC5000.getDesc());
        }
        return result;
    }

    /**
     * 根据id获取地址详情
     * @param id  地址id
     * @return
     */
    @RequestMapping("/getById")
    @ResponseBody
    public ResultEntity<Address> getById(String id){
        ResultEntity<Address> result = new ResultEntity<Address>();
        try {
            Address address = addressService.get(id);
            result.setStatusCode(StatusCode.SC0.getValue());
            result.setMsg(StatusCode.SC0.getDesc());
            result.setData(address);
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatusCode(StatusCode.SC4002.getValue());
            result.setMsg(StatusCode.SC4002.getDesc());
        }
        return result;
    }

    /**
     *添加或修改地址
     * @param address(realName:收货人,phone:手机号码,provinceName:所在城市,areaName:所在区域,detailAddress:详细地址)
     * @param request  accessToken
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addOrUpdate",method = RequestMethod.POST)
    public ResultEntity<Address> addOrUpdate(Address address,HttpServletRequest request){
        ResultEntity<Address> result = new ResultEntity<>();
        String accountId=(String)request.getAttribute("accountId");
        try {
            //信息填写不完整
            if (StringUtils.isEmpty(address.getPhone())||StringUtils.isEmpty(address.getRealName())||StringUtils.isEmpty(address.getDetailAddress())||StringUtils.isEmpty(address.getAreaName())){
                result.setStatusCode(StatusCode.SC4001.getValue());
                result.setMsg(StatusCode.SC4001.getDesc());
                return result;
            }
            if (address.getAsDefault()==1){
                addressService.setUnDefault(accountId);
            }
            address.setAccountId(accountId);
            addressService.save(address);
            result.setStatusCode(StatusCode.SC0.getValue());
            result.setMsg(StatusCode.SC0.getDesc());
            result.setData(address);
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatusCode(StatusCode.SC5000.getValue());
            result.setMsg(StatusCode.SC5000.getDesc());
        }
        return result;
    }
}